<?php $this->Html->addCrumb('My Favorite Sellers'); ?>
<?php //pr($sellers);?>
<style type="text/css">
    .noteButton{
        margin-left: 10px;
}
</style>
<div id="ajaxLoad">
    <div id="reload">
        <div class="search-title">My Favorite Sellers (<?php echo count($sellers);?> sellers)</div>
        <div class="product-info">
            <div class="porduct-con">
                <a elmid="" controller="users" action="add_favorite_note" title="Add Note" onclick="if(jQuery(this).attr('elmid')==''){alert('Selection is empty');}else{showPop(this)}" href="javascript:void(0);">
                    <input type="button" name="add_note_to_selected" value="Add note to selected">
                </a>

                <form name="favoriteSeller" action="" id="favoriteSellerForm">
                    <table cellspacing="0" cellpadding="0" border="0" width="100%" class="table-info-1">
                        <tr>
                            <td>
                                <input type="checkbox" name="selectAll">
                            </td>
                            <td>Seller</td>
                            <td>Added On</td>
                            <td>Email Subscription (Last Email Sent) </td>
                            <td>Period</td>
                            <td>Action</td>
                        </tr>
                        <?php foreach ($sellers as $seller):?>
                        <tr>
                            <td>
                                <input type="checkbox" name="data[FavoriteSeller][id][]" value="<?php echo $seller['FavoriteSeller']['id']; ?>" class="selectItem">
                            </td>
                            <td>
                                <?php echo $this->Html->link($seller['User']['username'], array('controller'=>'products', 'action'=>'store', $seller['User']['id']));?>
                            </td>
                            <td>
                                <?php echo date("m-d-Y", strtotime($seller['FavoriteSeller']['added_on']));?>
                            </td>
                             <td id="<?php echo $seller['subs']['Subscribe']['id'];?>">
                                 <?php if($seller['subs']['Subscribe']['subscription']==1){
                                    $elmid=0;
                                    $text= 'Unsubscribe';
                                 }else{
                                     $elmid=1;
                                    $text= 'Subscribe';
                                 }
                                 $lastMessege=$seller['lastMassegeRecive']==0?'No mail send':date("m-d-Y", $seller['lastMassegeRecive']);
                                 ?>
                                 <span>
                                     <a elmid="<?php echo $elmid; ?>" subId="<?php echo $seller['subs']['Subscribe']['id'];?>" seller_id="<?php echo $seller['subs']['Subscribe']['seller_id'];?>" title="<?php echo $text;?>" onclick="subsribe(this)" href="javascript:void(0);">
                                        <?php echo $text ?>
                                     </a>
                                     (<?php echo $lastMessege;?>)
                                 </span>
                             </td>
                            <td>
                                <select name="subscribe" onchange="period(this)" subId="<?php echo $seller['subs']['Subscribe']['id'];?>">
                                    <option value="30" <?php if($seller['subs']['Subscribe']['period']==30):?>selected="selected"<?php endif;?>>1 Month</option>
                                    <option value="7" <?php if($seller['subs']['Subscribe']['period']==7):?>selected="selected"<?php endif;?>>7 Days</option>
                                    <option value="15" <?php if($seller['subs']['Subscribe']['period']==15):?>selected="selected"<?php endif;?>>15 Days</option>
                                </select>
                            </td>
                            <td>
        <!--                        <a elmid="<?php //echo $seller['FavoriteSeller']['id']; ?>" controller="users" action="add_favorite_note" title="Add Note" onclick="showPop(this)" href="javascript:void(0);">-->
                                    <input type="button" name="addNote" value="Add Note">
        <!--                        </a>-->
                            </td>
                        </tr>
                        <?php if(count ($seller['FavouriteNote'])!=0):?>
                        <?php foreach($seller['FavouriteNote'] as $note):?>
                        <tr>
                            <td style="background-color: yellow" colspan="6">
                                <?php echo $note['note'];?>
                                <input type="hidden" name="data[FavouriteNote][id][]" value="<?php echo $note['id']?>">
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php endif;?>
                        <?php endforeach; ?>
                        <tr>
                            <td colspan="3">
                                <input type="button" name="remove_from_favorites" value="Remove from Favorites" />
                            </td>
                        </tr>
                    </table>
                </form>
               <div class="clear"></div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var content='<tr colspan="4"><td colspan="4"><input type="text" name="data[FavouriteNote][note]" class="favoriteNote" size="50"><input type="button" value="Save" name="sendMessege" class="noteButton" /><input type="button" value="Cancel" name="noteCancel" class="noteButton" /></td></tr>';
    jQuery('[name="selectAll"]').live('click', function(){
        if(jQuery(this).is(':checked')){
            jQuery(".selectItem").attr('checked', true);
        }else{
            jQuery(".selectItem").attr('checked', false);
        }
    });

    jQuery('[name="remove_from_favorites"]').live('click', function(){
        var data=jQuery("#favoriteSellerForm").serialize();
        var url=baseurl+"users/remove_favorite";
        var container="#ajaxLoad";
        var reload = "#reload";

        ajaxLoading(url, data, container, reload);
    });
    jQuery('[name="addNote"]').live('click', function(){
        jQuery(content).insertAfter(jQuery(this).parent().parent());
    });
    jQuery('[name="noteCancel"]').live('click', function(){
        jQuery(this).parent().parent().remove();
    });
    jQuery('input:button[name="sendMessege"]').live('click', function(){
        var url=baseurl+"/users/save_favorite_note";
        var data={"data[FavouriteNote][note]":jQuery(this).prev().val(), "data[FavouriteNote][favorite_sellers_id]":jQuery(this).closest('tr').prev().find('.selectItem').val()};
        var container="#ajaxLoad";
        var reload = "#reload";

        ajaxLoading(url, data, container, reload);
        return false;
    });

    jQuery('.selectItem, [name="selectAll"]').live('click', function(){
        var id = new Array();
        jQuery('.selectItem:checked').each(function(i){
            id[i]=jQuery(this).val();
        });
        jQuery('[action="add_favorite_note"]').attr('elmid', id);

    });

    function subsribe(obj){
        var elmid=jQuery(obj).attr("elmid");
        var id=jQuery(obj).attr("subId");
        var url=baseurl+"/users/toggleSubscribe";
        var data={"data[Subscribe][subscription]": elmid, "data[Subscribe][id]": id};
        var container="#"+id+"";
        var reload = "#"+id+" span";

        ajaxLoading(url, data, container, reload);
    }

    function period(obj){
        var id=jQuery(obj).attr('subId');
        var value=jQuery(obj).val();
        var data={"data[Subscribe][period]": value, "data[Subscribe][id]": id};
        var url= baseurl+"/users/toggleSubscribe";
        var container="#ajaxLoad";
        var reload = "#reload";

        ajaxLoading(url, data, container, reload);
    }

    function ajaxLoading(url, data, container, reload){
        jQuery.ajax({
            type: "post",
            url: url,
            data: data,
            success: function(){
                jQuery(container).load(""+baseurl+"users/favorite_seller "+reload+"");
            }
        })
    }
    
</script>